package be.destin.rdf.changes;

import java.util.Collection;

import be.destin.skos.html.SkosHtmlManager;
import be.destin.util.Util;

public class StatusFilter {

	Collection<? extends StatusInterface> baseList = null;
	int[] counts = null;
	
	static final int ordinalNA = Status.values().length;
	
	public StatusFilter (Collection<? extends StatusInterface> aList) {
		baseList = aList;
	}
	
	public void getFiltered(Status desired, Collection<StatusInterface> result) {
		for (StatusInterface si : baseList) {
			if (si.getStatus().equals(desired)) result.add(si);
		}
	}

	public void filter() {
		counts = new int[Status.values().length+1];
		countFiltered();
	}

	private int[] countFiltered() {
		for (StatusInterface si : baseList) {
			Status aStatus = si.getCurrentStatus();
			int ordinal;
			if (aStatus == null) ordinal = ordinalNA;
			else ordinal = aStatus.ordinal();
			counts[ordinal]++;
		}
		return counts;
	}
	
	public String toHtmlSearchFilters (SkosHtmlManager skosHtmlManager, String aParams) {
		if (counts==null) {
			filter();
		}
		String result = "";
		String sep = "";
		for (int i = 0; i < Status.values().length; i++) {
			int aCount = counts[i];
			if (aCount <= 0) continue;
			String completedParams = Util.replaceAllNoRegExp(aParams, "[status]", Status.values()[i].name());
			result += sep;
			sep = ", ";
			result += Status.values()[i].toHtml(skosHtmlManager)+":<a "+completedParams+"\">"+aCount+"</a>";
		}
		int aCount = counts[counts.length-1];
		if (aCount > 0) {
			String completedParams = Util.replaceAllNoRegExp(aParams, "[status]", "null");
			result += sep;
			result += "n.a:<a "+completedParams+"\">"+aCount+"</a>";
		}
		return result;
	}
	
	public int getCount (int ordinal) {
		if (counts==null) {
			filter();
		}
		return counts[ordinal];
	}
}
